<template>
    <div>
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
          <van-list id="todoList" v-model="loading" :finished="finished"
              :finished-text='!haveTodoError ? "没有更多数据" : ""' :error-text="todoErromessage" @load="onLoad"
              style="margin-bottom: 50px;">
              <van-cell v-for="(item, index) in list" :key="index" @click="getdetail(item)">
                  <div class="titlefont"> {{ item.businessName }}</div>
                  <div class="infosfont"> 商机编号:{{ item.businessId }}__{{ item.businessStatus }}</div>
                  <div class="infosfont"> {{ item.city }}__{{ item.county }}</div>
              </van-cell>
              <div v-show="!haveTodoData" class="text-c">
                  <img width="100px" height="100px" :src="require('@/assets/images/pic_nodata.png')" alt="暂无数据"
                      style="margin: 120px 50px 0" @click="getListdata()" />
              </div>

              <div v-show="haveTodoError" class="text-c">
                  <img width="100px" height="100px" :src="require('@/assets/images/pic_neterror.png')"
                      alt="获取数据失败" style="margin: 120px 50px 0" @click="getListdata()" />
                  <div class="color-b font14" style="margin: 20px 0 0 15px">
                      获取数据失败!
                  </div>
              </div>
          </van-list>
      </van-pull-refresh>
    </div>
</template>
<script>
import { findWholeNetworkAll } from "@/api/dictapi";
import { Toast } from "vant";
import { Notify } from "vant";
import { mapGetters } from "vuex";
import moment from 'moment'

export default {
    name: 'sjlistview',
    data() {
        return {
            list: [],
            refreshing: false,
            loading: false,
            finished: false,
            gettodoListQuery: { source: "MOBILE", page: 1, rows: 10, size: 10 },
            maxpagetodo: 100,
            todoErromessage: '',
            haveTodoData: true,
            businessId: '',
            projectName:'',
            haveTodoError: false,
        }
    },
    computed: {
        ...mapGetters(["user"]),
        ...mapGetters(["userinfo"]),
        ...mapGetters(["hassetmenu"]),
    },
    created() {
      this.onRefresh()
    },
    mounted() {
    },
    activated(){
      this.onRefresh()
    },
    methods: {
      //查询
      setsearchdata(businessId,projectName) {
        this.businessId = businessId;
        this.projectName = projectName; 
        this.onRefresh();
      },
      onRefresh() {
          //清空列表数据
          this.finished = false;
          //重新加载数据
          //将loading设置为true,表示处于加载状态
          this.loading = true;
          this.refreshing = true;
          this.gettodoListQuery.page = 1;
          this.maxpagetodo = 100;
          this.onLoad();
      },
      onLoad() {
          // setTimeout(() =>{
          if (this.refreshing) {
              this.list = [];
              this.refreshing = false
          }
          if (this.gettodoListQuery.page <= this.maxpagetodo) {
              findWholeNetworkAll(Object.assign(this.gettodoListQuery,
                  { userName: this.user.userName },
                  { businessId: this.businessId },
                  { projectName: this.projectName },
                  { model: 'ybzc' }
              ))
                  .then((result) => {
                      this.haveTodoError = false;
                      if (!result.data) {
                          this.loading = false;
                          this.finished = true;
                          Notify(result.message);
                          this.todoErromessage = result.message;
                          return;
                      }
                      let listdetails = this.list;
                      if (this.gettodoListQuery.page == 1) {
                          listdetails = [];
                          if (result.data.data){
                            if (result.data.data.content) {
                              listdetails = result.data.data.content;
                            }
                            if (listdetails.length == 0) {
                                this.haveTodoData = false;
                            } else {
                                this.haveTodoData = true;
                            }
                            this.maxpagetodo = result.data.data.totalPages;
                          }else{
                            this.haveTodoData = false;
                          }
                      } else {
                          if (result.data.data.content) {
                              listdetails = listdetails.concat(result.data.data.content);
                          }
                      }

                      this.list = listdetails;
                      this.loading = false;
                      this.finished = false;
                      this.gettodoListQuery.page = this.gettodoListQuery.page + 1;
                  })
                  .catch((error) => {
                      console.log("22222222222" + JSON.stringify(error));
                      this.loading = false
                      this.finished = true;
                      this.haveTodoData = true;
                      this.todoErromessage = '获取数据失败,请稍后重试';
                      this.haveTodoError = true,
                          Notify({ type: 'warning', message: '获取数据失败,请稍后重试' })
                  });
          } else {
              this.loading = false
              this.finished = true;
          }
          // },1000);
      },
      timeUtil(value) {
          return moment(value).format('YYYY-MM-DD HH:mm:ss');
      },

      getListdata() {
        this.onRefresh();
      },

      getdetail(value) {
        console.log("条目内容是"+JSON.stringify(value))
        localStorage.setItem('sjlistdata', JSON.stringify(value))
        this.$router.back(-1)
      }
    }
}
</script>
<style scoped>
.btn {
    margin-bottom: 60px;
    margin-top: 60px;
}

.custom-image .van-empty__image {
    width: 90px;
    height: 90px;
}
</style>